package com.server.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.server.annotation.NoAccessClient;
import com.server.component.RedisUtil;
import com.server.entity.*;
import com.server.param.UpmsUserVo;
import com.server.service.IUpmsRoleService;
import com.server.service.IUpmsUserOrganizationService;
import com.server.service.IUpmsUserService;
import com.server.util.BaseController;
import com.server.util.Result;
import com.server.vo.upms.login.LoginVo;
import com.server.vo.upms.user.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户 控制器
 * </p>
 *
 * @author
 * @since 2020-04-03
 */
@RestController
@Controller
@AllArgsConstructor
@Slf4j
@Api(value = "后台用户管理", tags = "后台用户管理")
@RequestMapping("/admin/upmsUser")
@Validated
public class AdminUpmsUserController extends BaseController {

    private IUpmsUserService upmsUserService;
    private IUpmsRoleService upmsRoleService;
    private IUpmsUserOrganizationService upmsUserOrganizationService;
    private RedisUtil redisUtil;

    /**
     * 新增
     *
     * @author
     * @Date 2020-04-03
     */
    @NoAccessClient
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ResponseBody
    public Result<UserInfo> login(@ApiParam(name = "loginVo", value = "登录对象")@Valid @RequestBody LoginVo vo) {
        return  upmsUserService.login(vo);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2020-04-03
     */
    @GetMapping("/getListPage")
    @ApiOperation(value = "查询列表")
    @ResponseBody
    public Result<List<UpmsUser>> listPage(@ApiParam(name = "username", value = "用户名", required = false)@RequestParam(required = false) String username,
                                           @ApiParam(name = "phone", value = "电话", required = false)@RequestParam(required = false) String phone,
                                           @ApiParam(name = "locked", value = "锁定状态(1:正常,0:锁定)", required = false)@RequestParam(required = false,defaultValue = "1") Integer locked,
                                           @ApiParam(name = "enable", value = "是否启用(1:启用,0:禁用)", required = false)@RequestParam(required = false,defaultValue = "1") Integer enable,
                                           @ApiParam(name = "curPage", value = "当前页(1不传入则默认1)", required = false)@RequestParam(required = false,defaultValue = "1") long curPage,
                                           @ApiParam(name = "pageSize", value = "当前页大小(1不传入则默认10)", required = false)@RequestParam(required = false,defaultValue = "10") long pageSize) {
        Page<UpmsUser> page = new Page<>(curPage,pageSize);
        QueryWrapper<UpmsUser> q = new QueryWrapper<>();
        if(StrUtil.isNotBlank(username)){
            q.lambda().like(UpmsUser::getUsername,username);
        }
        if(StrUtil.isNotBlank(phone)){
            q.lambda().like(UpmsUser::getPhone,phone);
        }
        if(locked!=null){
            q.lambda().eq(UpmsUser::getLocked,locked);
        }
        if(enable!=null){
            q.lambda().eq(UpmsUser::getEnable,enable);
        }
        return upmsUserService.findPage(page,q);
    }

    /**
     * 根据id查询详情
     *
     * @author
     * @Date 2020-04-03
     */
    @GetMapping("/findById")
    @ApiOperation(value = "根据id查询详情")
    @ResponseBody
    public Result<UpmsUserVo> findById(@ApiParam(name = "id", value = "用户id", required = true)@RequestParam String id) {
        UpmsUser user = upmsUserService.findById(id);
        if(user==null){
            return Result.error(HttpStatus.HTTP_UNAVAILABLE,"找不到对应用户");
        }
        UpmsUserVo vo = new UpmsUserVo();
        BeanUtil.copyProperties(user,vo);
        vo.setPassword(null);
        return Result.returnData(vo);
    }

    /**
     * 新增
     *
     * @author
     * @Date 2020-04-03
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    @ResponseBody
    public Result add(@ApiParam(name = "vo", value = "新增对象")@Valid @RequestBody UpmsUserVo vo) {
        return upmsUserService.add(vo);
    }

    /**
     * 修改
     *
     * @author
     * @Date 2020-04-03
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @ResponseBody
    public Result update(@ApiParam(name = "vo", value = "修改对象")@Valid @RequestBody UpmsUserVo vo) {
        if(vo.getUserId()==null){
            return Result.error("用户id不能为空");
        }

        return  upmsUserService.update(vo);
    }


    /**
     * 禁用
     *
     * @author
     * @Date 2020-04-03
     */
    @PostMapping("/enable")
    @ApiOperation(value = "禁用")
    @ResponseBody
    public Result update(@ApiParam(name = "ids", value = "ids") @RequestParam String  ids,
                         @ApiParam(name = "enable", value = "修改对象") @RequestParam Integer  enable) {

        return  upmsUserService.enable(ids,enable);
    }


    /**
     * 批量删除
     *
     * @author
     * @Date 2020-04-03
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "批量删除")
    @ResponseBody
    public Result delete(@ApiParam(name = "ids", value = "批量删除", required = true)@Valid @RequestParam String ids) {
        return upmsUserService.delete(ids)?Result.ok("操作成功"):Result.error("操作失败");
    }

    /**
     * 根据userId查询用户所在组织
     *
     * @author
     * @Date 2020-04-03
     */
    @GetMapping("/findOrganizationByUserId")
    @ApiOperation(value = "根据userId查询用户所在组织")
    @ResponseBody
    public Result<List<UpmsOrganization>> findOrganizationByUserId(@ApiParam(name = "id", value = "用户id", required = true)@RequestParam String id) {

        List<UpmsOrganization> list = upmsUserOrganizationService.findOrganizationByUserId(id);
        return Result.returnData(list);
    }


    /**
     * 根据userId查询用户角色
     *
     * @author
     * @Date 2020-04-03
     */
    @GetMapping("/findRoleByUserId")
    @ApiOperation(value = "根据userId查询用户角色")
    @ResponseBody
    public Result<List<UpmsRole>> findRoleByUserId(@ApiParam(name = "id", value = "用户id", required = true)@RequestParam String id) {
        List<UpmsRole> list = upmsRoleService.findRoleByUserId(id);
        return Result.returnData(list);
    }
}
